<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 他人空间控制器
 */
class Space extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->uuid = $this->action;
        $this->suser_id = decrypt_user_id($this->uuid);
        
        if($this->user_id && $this->user_id == $this->suser_id) redirect("account");
        $this->assign('uuid', $this->uuid);
        $this->assign("space_url",  site_url("u/".$this->uuid));
    }
    
    
    
    //他的首页
    function index($uuid){
        //基本信息
        $query_params = "user_id,nickname,head_img,sex,signature";
        $user_info = $this->db->query("select $query_params from tan_users_info where user_id = ".$this->suser_id." order by id desc limit 1")->row_array();
        if(!$user_info){
            $this->display('deleted/index.html');exit;
        }
        $user_info['signature'] = $user_info['signature'] ? $user_info['signature'] : "这个人好懒，什么也没有写～";
        $user_info['head_img'] = strstr($user_info['head_img'],"http") ? $user_info['head_img'] : $this->img_host.$user_info['head_img'];
        //统计信息
        $user_count = $this->db->query("select * from tan_bbs_count where user_id = ".$this->suser_id." order by id desc limit 1")->row_array();
        $user_count['articles_count'] = $this->db->query("select * from tan_bbs_article where authorid = ".$this->suser_id." and  status = 1 order by id desc")->num_rows();
        $user_count['reply_count'] = $this->db->query("select * from tan_bbs_article_comment where uid = ".$this->suser_id." and  is_show = 1 order by id desc")->num_rows();
        $user_info['bbs_count'] = $user_count;
        $is_fan = $this->user_id ? $this->bbs_user_model->get_fan_status(array("user_id"=>$this->suser_id,"followuid"=>$this->user_id,"is_del"=>1)) : 0;
        $user_info['is_fan'] = $is_fan ? 1 : 0;
        
        //他的文章
        $article_list = $this->article_list();
        //他的问答
        $wenda_list = $this->wenda_list();
        $this->assign("article_list", $article_list);
        $this->assign("wenda_list", $wenda_list);
        $this->assign("user_info", $user_info);
        $this->display("space/index.html");
    }
    
    private function article_list(){
        $query_params = "id,authorid,title,content,thumb,readnum,commentnum,create_time";
        $list = $this->db->query("select $query_params from tan_bbs_article where `authorid` = ".$this->suser_id." and `status` = 1 and `show` = 1 order by hot desc,sort desc,id desc limit 5")->result_array();
        foreach ($list as $key=>$val){
            $matches = array();
            preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/i", $val['content'], $matches);
            $content_thumb = $matches[1][0] ? $matches[1][0] : "";
            $list[$key]['content_thumb'] = $content_thumb;
            $list[$key]['detail_url'] = site_url("p/".  encrypt_article_id($val['id']));
            $list[$key]['article_id'] = encrypt_article_id($val['id']);
            $list[$key]['create_time'] = $this->set_time($val['create_time']);
        }
        return $list;
    }
    
    private function wenda_list(){
        $query_params = "id,questioner_id,title,answernum,readnum,focusnum,create_time";
        $list = $this->db->query("select $query_params from tan_bbs_wenda where `questioner_id` = ".$this->suser_id." and `status` = 1 and `is_show` = 1 order by hot desc,sort desc,id desc limit 5")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['detail_url'] = site_url("wenda/". encrypt_wenda_id($val['id']));
            $list[$key]['create_time'] = $this->set_time($val['create_time']);
        }
        return $list;
    }
    
    //他的文章
    function arts(){
        $this->display('space/article.html');
    }
    //他的关注
    function follows($uuid){
        $list = $this->db->query("select f.*,i.nickname,i.head_img,i.signature from tan_bbs_fans f left join tan_users_info i on i.user_id = f.user_id where f.followuid = ".$this->suser_id." and f.is_del = 1 order by f.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $is_fan = $this->user_id ? ($this->bbs_user_model->get_fan_status(array("user_id"=>$val['user_id'],"followuid"=>$this->user_id,"is_del"=>1)) ? 1 : 0) : 0;
            $list[$key]['uuid'] = encrypt_user_id($val['user_id']);
            $list[$key]['space_url'] = site_url("u/".encrypt_user_id($val['user_id']));
            $list[$key]['signature'] = $val['signature'] ? $val['signature'] : "这个人好懒，什么也没有写～";
            $list[$key]['is_fan'] = $is_fan;
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
        }
        $this->assign("list", $list);
        $this->display("space/follow.html");
    }
    //他的粉丝
    function fans($uuid){
        $list = $this->db->query("select f.*,i.nickname,i.head_img,i.signature from tan_bbs_fans f left join tan_users_info i on i.user_id = f.followuid where f.user_id = ".$this->suser_id." and f.is_del = 1 order by f.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['uuid'] = encrypt_user_id($val['followuid']);
            //验证是否互相关注
            $is_fan = $this->user_id ? ($this->bbs_user_model->get_fan_status(array("user_id"=>$val['user_id'],"followuid"=>$this->user_id,"is_del"=>1)) ? 1 : 0) : 0;
            $is_fan = $is_fan ? 1 : 0;
            $list[$key]['space_url'] = site_url("u/".encrypt_user_id($val['followuid']));
            $list[$key]['signature'] = $val['signature'] ? $val['signature'] : "这个人好懒，什么也没有写～";
            $list[$key]['is_fan'] = $is_fan;
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
        }
        $this->assign("list", $list);
        $this->display("space/fan.html");
    }
    //他的提问
    function question($uuid){
        
        $this->assign("nav", "question");
        $this->display('space/question.html');
    }
    //他的回答
    function answer($uuid){
        
        $this->assign("nav", "answer");
        $this->display('space/answer.html');
    }
    //他的评论
    function comment(){
        $list = $this->db->query("select c.*,i.nickname,i.head_img,a.title,a.commentnum from tan_bbs_article_comment c left join tan_users_info i on i.user_id = c.authorid left join tan_bbs_article a on a.id = c.article_id where c.uid = ".$this->suser_id." and c.is_show = 1 order by c.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]["space_url"] = site_url("u/".  encrypt_user_id($val['authorid']));
            $list[$key]["detail_url"] = site_url("p/".  encrypt_article_id($val['article_id']));
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]["create_time"] = date("m-d H:s",$val['create_time']);
        }
        $this->assign("list", $list);
        $this->display('space/comments.html');
    }
}
